package com.winit.openapi.wh.builder;

import java.util.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ibm.icu.text.SimpleDateFormat;
import com.winit.oms.spi.inbound.vo.InboundReservationPackageTypeVo;
import com.winit.oms.spi.inbound.vo.ReservationSendWhOrderVo;
import com.winit.oms.spi.inbound.vo.WaitForReservationOrderVo;
import com.winit.openapi.util.DateUtil;

/**
 * 预约送仓单构建器类
 * 
 * @version <pre>
 * Author	  Version		Date		Changes
 * dinghua.an	1.0  		2015年12月3日 	Created
 *
 * </pre>
 * @since 1.
 */
public class ReservationSendWhBuilder {

    /**
     * 查询待预约直发订单接口出参封装
     * 
     * @param list
     * @return
     */
    public static List<Map<String, Object>> buildWaitingReservationList(List<WaitForReservationOrderVo> list) {
        List<Map<String, Object>> mList = new ArrayList<Map<String, Object>>();
        for (WaitForReservationOrderVo vo : list) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("orderNo", vo.getOrderNo());
            map.put("inspectionWhCode", vo.getInspectionWhCode());
            map.put("inspectionWhName", vo.getInspectionWhName());
            map.put("destinationWhCode", vo.getDestWhCode());
            map.put("destinationWhName", vo.getDestWhName());
            map.put("orderVolume", vo.getOrderVolume());
            map.put("orderType", vo.getOrderType());
            mList.add(map);
        }
        return mList;
    }

    /**
     * 创建预约单 vo构建
     * 
     * @param json
     * @return
     */
    public static ReservationSendWhOrderVo buildCreateReservationOrder(JSONObject json) {
        ReservationSendWhOrderVo vo = new ReservationSendWhOrderVo();
        if (json.containsKey("destinationWhCode")) {
            vo.setDestinationWhCode(json.getString("destinationWhCode").trim());
        }
        if (json.containsKey("destinationWhName")) {
            vo.setDestinationWhName(json.getString("destinationWhName").trim());
        }
        if (json.containsKey("contactPerson")) {
            vo.setContactPerson(json.getString("contactPerson").trim());
        }
        if (json.containsKey("contactPhone")) {
            vo.setContactPhone(json.getString("contactPhone").trim());
        }
        if (json.containsKey("advanceBookingTime")) {
            vo.setAdvanceBookingTime(json.getInteger("advanceBookingTime"));
        }
        //分割日期
        if (json.containsKey("sendWhDate")) {
            String sendWhDate=json.getString("sendWhDate");
            String[] dates=sendWhDate.split("\\|");
            vo.setSendWhStartDate(DateUtil.parse2Date(dates[0],DateUtil.FULL_DATE_STRING));
            vo.setSendWhEndDate(DateUtil.parse2Date(dates[1],DateUtil.FULL_DATE_STRING));
        }
        List<WaitForReservationOrderVo> wlist = new ArrayList<WaitForReservationOrderVo>();
        if (json.containsKey("orderNoList")) {
            JSONArray orderNoList = json.getJSONArray("orderNoList");
            for (Object object : orderNoList) {
                JSONObject orderInfo = (JSONObject) object;
                WaitForReservationOrderVo rdVo = new WaitForReservationOrderVo();
                if (orderInfo.containsKey("orderNo")) {
                    rdVo.setOrderNo(orderInfo.getString("orderNo").trim());
                }
                if (orderInfo.containsKey("inspectionWhCode")) {
                    rdVo.setInspectionWhCode(orderInfo.getString("inspectionWhCode").trim());
                }
                if (orderInfo.containsKey("inspectionWhName")) {
                    rdVo.setInspectionWhName(orderInfo.getString("inspectionWhName").trim());
                }
                wlist.add(rdVo);
            }
        }
        vo.setReservationOrderVoList(wlist);
        List<InboundReservationPackageTypeVo> packageTypeVoList = new ArrayList<InboundReservationPackageTypeVo>();
        if (json.containsKey("packageTypeList")) {
            JSONArray packageTypeList = json.getJSONArray("packageTypeList");
            for (Object object : packageTypeList) {
                JSONObject packageType = (JSONObject) object;
                InboundReservationPackageTypeVo tvo = new InboundReservationPackageTypeVo();
                if (packageType.containsKey("packageType")) {
                    tvo.setPackageType(packageType.getString("packageType").trim());
                }
                if(packageType.containsKey("listValueSource"))
                {
                    if("P".equals(packageType.getString("listValueSource")))
                    {
                        if (packageType.containsKey("quantity")) {
                            tvo.setQuantity(packageType.getIntValue("quantity"));
                        }
                    }else{
                        tvo.setQuantity(0);
                    }
                }
                packageTypeVoList.add(tvo);
            }
        }
        vo.setPackageTypeVoList(packageTypeVoList);
        return vo;
    }

    /**
     * 编辑预约单入参构建
     * 
     * @param json
     * @return
     */
    public static ReservationSendWhOrderVo buildUpdateReservationOrder(JSONObject json) {
        ReservationSendWhOrderVo vo = buildCreateReservationOrder(json);
        if (json.containsKey("reservationOrderNo")) {
            vo.setReservationOrderNo(json.getString("reservationOrderNo").trim());
        }
        return vo;
    }

    /**
     * 查询预约单详情 出参封装
     * 
     * @param list
     * @return
     */
    public static Map<String, Object> buildReservationOrderDetail(ReservationSendWhOrderVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("reservationOrderNo", vo.getReservationOrderNo());
        map.put("takeEffectDate", vo.getTakeEffectDate());
        map.put("destinationWhCode", vo.getDestinationWhCode());
        map.put("destinationWhName", vo.getDestinationWhName());
        map.put("destinationWhAddress", vo.getDestinationWhAddress());
        map.put("destinationWhPhone", vo.getDestinationWhPhone());
        //拼装日期
        String sendWhDate=buildDate(vo.getSendWhStartDate(),vo.getSendWhEndDate());
        map.put("sendWhDate", sendWhDate);
        map.put("contactPerson", vo.getContactPerson());
        map.put("contactPhone", vo.getContactPhone());
       // map.put("orderVolume", vo.getOrderVolume());
        //拼装入库单集合
        List<Map<String, Object>> orderMapList=new ArrayList<Map<String,Object>>();
        List<WaitForReservationOrderVo> orderVoList=vo.getReservationOrderVoList();
        if(orderVoList!=null)
        {
            for(WaitForReservationOrderVo wvo:orderVoList)
            {
                Map<String, Object> orderMap = new HashMap<String, Object>();
                orderMap.put("orderNo", wvo.getOrderNo());
                orderMap.put("inspectionWhCode", wvo.getInspectionWhCode());
                orderMap.put("inspectionWhName", wvo.getInspectionWhName());
                orderMap.put("volume", wvo.getVolume());
                orderMapList.add(orderMap);
            }
        }
        map.put("orderNoList", orderMapList);
       
        //拼装箱型列表
        List<Map<String, Object>> packageTypeMapList=new ArrayList<Map<String,Object>>();
        List<InboundReservationPackageTypeVo> packageTypeVoList=vo.getPackageTypeVoList();
        if(packageTypeVoList!=null)
        {
            for(InboundReservationPackageTypeVo pvo:packageTypeVoList)
            {
                Map<String, Object> packageTypeMap=new HashMap<String, Object>();
                packageTypeMap.put("packageType", pvo.getPackageType());
                packageTypeMap.put("quantity", pvo.getQuantity());
                packageTypeMapList.add(packageTypeMap);
            }
        }
        map.put("packageTypeList", packageTypeMapList);
        return map;
    }
    
    /**
     * 构建日期
     * @param startDate
     * @param endDate
     * @return
     */
    private static String buildDate(Date startDate,Date endDate){
        if(startDate!=null && endDate!=null)
        {
            SimpleDateFormat sdf = new SimpleDateFormat(DateUtil.FULL_DATE_STRING);
            StringBuilder sbBuilder=new StringBuilder();
            sbBuilder.append(sdf.format(startDate)).append("|");
            sbBuilder.append(sdf.format(endDate));
            return sbBuilder.toString();
        }
       return null;
    }

    /**
     * 查询预约单列表  出参封装
     * 货运代理
     * @param list
     * @return
     */
    public static Map<String, Object> buildReservationOrderList(ReservationSendWhOrderVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("reservationOrderNo", vo.getReservationOrderNo());
        map.put("createDate", vo.getCreateDate());
        //拼装日期
        String sendWhDate=buildDate(vo.getSendWhStartDate(),vo.getSendWhEndDate());
        map.put("sendWhDate", sendWhDate);
        map.put("contactPerson", vo.getContactPerson());
        map.put("contactPhone", vo.getContactPhone());
        //拼装入库单集合
        List<Map<String, Object>> orderMapList=new ArrayList<Map<String,Object>>();
        List<WaitForReservationOrderVo> orderVoList=vo.getReservationOrderVoList();
        if(orderVoList!=null)
        {
            for(WaitForReservationOrderVo wvo:orderVoList)
            {
                Map<String, Object> orderMap = new HashMap<String, Object>();
                orderMap.put("orderNo", wvo.getOrderNo());
                orderMapList.add(orderMap);
            }
        }
        map.put("orderNoList", orderMapList);
        return map;
    }
}
